package org.dromara.oce.utils;


import lombok.Data;
import lombok.experimental.UtilityClass;
import org.dromara.common.core.constant.GlobalConstants;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @desc: 平台参数转化工具类
 * @author: hhl
 * @date：2024/5/28 15:50
 */
@Data
@UtilityClass
public class PlatformParamTransUtil {

    // 部门数据切换的缓存key前缀
    private static final String DYNAMIC_DEPT_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "dynamicDept";
    /**
     * 设置动态部门id
     *
     * @param deptId 部门id
     */
    public static void setDynamicDept(Long deptId) {
        String cacheKey = DYNAMIC_DEPT_KEY + ":" + LoginHelper.getUserId();
        RedisUtils.setCacheObject(cacheKey, deptId);
    }

    /**
     * 获取动态部门id
     */
    public static Long getDynamicDept() {
        long deptId = 0L;
        String cacheKey = DYNAMIC_DEPT_KEY + ":" + LoginHelper.getUserId();
        Object deptIdCache = RedisUtils.getCacheObject(cacheKey);
        // 没有设置时给默认值0
        if (Objects.isNull(deptIdCache)) {
            setDynamicDept(deptId);
        }else {
            deptId = Long.parseLong(deptIdCache+"");
        }
        return deptId;
    }
    public Integer getPlatformNum(String platformType) {
        if (PlatformParamTransUtil.isKuaiShou(platformType)) {
            return PlatformConstants.KUAISHOU_NUM;
        } else if (PlatformParamTransUtil.isDouYin(platformType)) {
            return PlatformConstants.DOUYIN_NUM;
        } else if (PlatformParamTransUtil.isWechat(platformType)) {
            return PlatformConstants.WECHAT_NUM;
        }
        return 0;
    }


    public boolean isKuaiShou(String type) {
        return PlatformConstants.KUAISHOU.equals(type);
    }

    /**
     * 判断是否是快手平台
     *
     * @param platformNum 平台标识
     */
    public boolean isKuaiShouNum(Integer platformNum) {
        return PlatformConstants.KUAISHOU_NUM == platformNum;
    }

    /**
     * 判断是否是APP平台
     *web  app
     * @param platformNum 平台标识
     */
    public boolean isAppNum(Integer platformNum) {
        return PlatformConstants.APP_NUM == platformNum;
    }


    public boolean isApp(String type) {
        return PlatformConstants.APP.equals(type);
    }

    /**
     * 判断是否是网页平台
     *web  app
     * @param platformNum 平台标识
     */
    public boolean isWebNum(Integer platformNum) {
        return PlatformConstants.WEB_NUM == platformNum;
    }

    public boolean isWeb(String type) {
        return PlatformConstants.WEB.equals(type);
    }


    public boolean isDouYin(String type) {
        return PlatformConstants.DOUYIN.equals(type);
    }

    /**
     * 判断是否抖音平台
     *
     * @param platformNum 平台标识
     */
    public boolean isDouYinNum(Integer platformNum) {
        return PlatformConstants.DOUYIN_NUM == platformNum;
    }


    public boolean isWechat(String type) {
        return PlatformConstants.WECHAT.equals(type);
    }

    /**
     * 判断是否是微信平台
     *
     * @param platformNum
     * @return
     */
    public boolean isWechatNum(Integer platformNum) {
        return PlatformConstants.WECHAT_NUM == platformNum;
    }

    /**
     * 判断投广渠道是否是巨量
     * @return
     */
    public boolean isChannelJuLiang(String channelId) {
        return PlatformConstants.JULIANG.equals(channelId);
    }
    /**
     * 判断投广渠道是否是星图
     * @return
     */
    public boolean isChannelXingTu(String channelId) {
        return PlatformConstants.XINGTU.equals(channelId);
    }
    /**
     * 判断投广渠道是否是腾讯
     * @param channelId
     * @return
     */
    public boolean isChannelTengXun(String channelId){
        return PlatformConstants.TENCENT.equals(channelId);
    }


    /**
     * 判断订单是不是ios支付
     * @param devicePlatform
     * @return
     */
    public boolean isIosPay(String devicePlatform){
        if("ios".equals(devicePlatform)){
            return true;
        }
        return false;
    }


    /**
     * 根据平台类型，获取平台名称
     * @param type
     * @return
     */
    public String getPlatformName(String type){
        if(PlatformParamTransUtil.isWechat(type)){
            return "微信";
        }else if(PlatformParamTransUtil.isKuaiShou(type)){
            return "快手";
        }else if(PlatformParamTransUtil.isDouYin(type)){
            return "抖音";
        }
        return "未知平台";
    }

    public String getPlatformName(Integer platformNum){
        if(PlatformParamTransUtil.isWechatNum(platformNum)){
            return "微信";
        }else if(PlatformParamTransUtil.isKuaiShouNum(platformNum)){
            return "快手";
        }else if(PlatformParamTransUtil.isDouYinNum(platformNum)){
            return "抖音";
        }
        return "未知平台";
    }

    /**
     * 判断是否是iap链接类型
     * @param referralLinkType
     * @return
     */
    public boolean isIapReferralType(Integer referralLinkType){
        if (isReferralTypeDaiShu(referralLinkType) || isReferralTypeFanQie(referralLinkType)
                || isReferralTypeDianZhong(referralLinkType)) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否是iaa链接类型
     * @param referralLinkType
     * @return
     */
    public boolean isIaaReferralType(Integer referralLinkType){
        if (isReferralTypeDaiShuIaa(referralLinkType) || isReferralTypeFanQieIaa(referralLinkType)
                || isReferralTypeDianZhong(referralLinkType)) {
            return true;
        }
        return false;
    }

    /**
     * 袋鼠链接类型
     * @param referralLinkType
     * @return
     */
    public boolean isReferralTypeDaiShu(Integer referralLinkType){
        if(PlatformConstants.REFERRAL_LINK_TYPE_SELF.intValue() == referralLinkType.intValue()){
            return true;
        }
        return false;
    }

    /**
     * 袋鼠iaa
     * @param referralLinkType
     * @return
     */
    public boolean isReferralTypeDaiShuIaa(Integer referralLinkType){
        if(PlatformConstants.REFERRAL_LINK_TYPE_DAISHU_IAA.intValue() == referralLinkType.intValue()){
            return true;
        }
        return false;
    }

    /**
     * 番茄
     * @param referralLinkType
     * @return
     */
    public boolean isReferralTypeFanQie(Integer referralLinkType){
        if(PlatformConstants.REFERRAL_LINK_TYPE_FANQIE.intValue() == referralLinkType.intValue()){
            return true;
        }
        return false;
    }

    /**
     * 番茄iaa
     * @param referralLinkType
     * @return
     */
    public boolean isReferralTypeFanQieIaa(Integer referralLinkType){
        if(PlatformConstants.REFERRAL_LINK_TYPE_FANQIE_IAA.intValue() == referralLinkType.intValue()){
            return true;
        }
        return false;
    }

    /**
     * 点众
     * @param referralLinkType
     * @return
     */
    public boolean isReferralTypeDianZhong(Integer referralLinkType){
        if(PlatformConstants.REFERRAL_LINK_TYPE_DIANZHONG.intValue() == referralLinkType.intValue()){
            return true;
        }
        return false;
    }


    /**
     * 处理抖音视频特殊字符
     * @return
     */
    public String changeSpecialChar(String title){
        List<String> charList = Arrays.asList(" ","#","%","&","\\","\"","<",">","?","$","+",",","，");
        if(charList.contains(title)){
            for (String s : charList) {
                title = title.replace(s,"");
            }
        }
        return title;
    }

}
